package logic

import (
	"context"
	"errors"
	"video/models/mysql"
	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateNickNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateNickNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNickNameLogic {
	return &UpdateNickNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改昵称
func (l *UpdateNickNameLogic) UpdateNickName(in *videorpc.UpdateNickNameRequest) (*videorpc.UpdateNickNameResponse, error) {
	// todo: add your logic here and delete this line
	var FindUser mysql.User
	err := FindUser.FindUserByPhone(mysql.DB, in.Phone)
	if err != nil {
		return nil, err
	}
	if FindUser.ID == 0 {
		return nil, errors.New("用户未注册,不允许重置")
	}
	FindUser.NickName = in.NickName
	err = FindUser.UpdateNickName(mysql.DB)
	if err != nil {
		return nil, errors.New("修改失败")
	}
	return &videorpc.UpdateNickNameResponse{
		UserId: int64(FindUser.ID),
	}, nil
}
